﻿//-----------------------------------------------------------------------
// <copyright file="AmplifyExtensions.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//---------------------------------------------------------------------
namespace Deixei.Web
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Globalization;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    using System.Web.Routing;
    using System.Web.Script.Serialization;
    using Deixei;
    
    using Deixei.Domain;
    using Deixei.Web.Attributes;

    public static class AmplifyExtensions
    {


//amplify.request.define("GetNavigationAreasByPage", "ajax", {
//    url: "/Admin/Portfolio/GetNavigationAreasByPage",
//    dataType: "json",
//    type: "GET",
//    cache: false,
//    decoder: "RegularData"
//});


        private static string AmpDefineRequestJavascript(string Action, string Controller, string Area, string dataType = "json", string type = "GET", string cache = "false", string decoder = "RegularData")
        {
            var sb = new StringBuilder();
            sb.AppendFormat(@"amplify.request.define(""{0}"", ""ajax"", {{", Action);
            if ( string.IsNullOrEmpty(Area) )
            {
                sb.AppendFormat(@"url: ""/{0}/{1}"",", Controller, Action);
            }
            else
            {
                sb.AppendFormat(@"url: ""/{0}/{1}/{2}"",", Area, Controller, Action);
            }
            sb.AppendFormat(@"dataType: ""{0}"",", dataType);
            sb.AppendFormat(@"type: ""{0}"",", type);
            sb.AppendFormat(@"cache: {0},", cache);
            sb.AppendFormat(@"decoder: ""{0}""", decoder);
            sb.Append(string.Format("}});"));
            return sb.ToString();
        }


        public static MvcHtmlString AmpDefineRequest(this HtmlHelper html, string Action, string Controller, string Area, string dataType = "json", string type = "GET", string cache ="false", string decoder ="RegularData")
        {
            return MvcHtmlString.Create(AmpDefineRequestJavascript(Action, Controller, Area, dataType, type, cache, decoder));
        }

        public static MvcHtmlString AmpDefineRequests(this HtmlHelper html, Type controller) 
        {
            StringBuilder sb = new StringBuilder();
            string action;
            string controllerName = html.ViewContext.DeixeiWebContext().CurrentController;
            string area = html.ViewContext.DeixeiWebContext().CurrentArea;
            string dataType = "json";
            string type = "GET";
            string cache ="false";
            string decoder = "RegularData";

            bool HasPostType = false;
            bool HasAjaxRequestAttribute = false;

            MethodInfo[] methodInfo = controller.GetMethods();

            foreach ( MethodInfo item in methodInfo )
            {

                HasPostType = item.CustomAttributes.Where(x => x.AttributeType == typeof(HttpPostAttribute)).FirstOrDefault() != null;
                HasAjaxRequestAttribute = item.CustomAttributes.Where(x => x.AttributeType == typeof(AjaxRequestAttribute)).FirstOrDefault() != null;

                if ( item.ReturnType == typeof(JsonResult) && HasAjaxRequestAttribute )
                {
                    action = item.Name;

                    type = HasPostType ? "POST" : "GET";

                    sb.AppendLine(AmpDefineRequestJavascript(action, controllerName, area, dataType, type, cache, decoder));
                }
            }

            return MvcHtmlString.Create(sb.ToString());
        }


    }
}
